var http = require('http');
var getMediaInfo = require('./getMediaInfo.js');
var videoCoverWidth = 448;
var videoCoverHeight = 336;
module.exports = {
    'video':function (uri, model, callback) {
        var type = (function () {
            if (uri.indexOf('v.youku.com') > -1) {
                return 'youku';
            }
            if (uri.indexOf('www.tudou.com') > -1) {
                return 'tudou';
            }
        })();
        getMediaInfo[type](uri, function (data) {
            model.title = data.title;
            if (data.description) {
                model.description = data.description;
            }
            model.tags = data.tags;
            /** @namespace data.img */
            /** @namespace data.vid */
            model.mediaCover = data.img;
            model.source = uri;
            model.sourceName = data.type;
            model.mediaUri = data.vid;
            model.mediaCoverWidth = data.imgWidth;
            model.mediaCoverHeight = data.imgHeight;
            callback();
        });
    },
    'image':function (uri, model, callback) {
        var options = {
            host:'rekey.sinaapp.com',
            port:80,
            path:'/image.php?url=' + uri
        };
        http.get(options, function (res) {
            res.on('data', function (data) {
                data = JSON.parse(data.toString());
                if (data.stats != 'done') {
                    res.render('media/error500');
                    return;
                }
                /** @namespace data.img */
                /** @namespace data.img.h */
                /** @namespace data.thumb */
                model.sourceName = url.parse(model.source).hostname;
                model.mediaUri = data.img.url;
                model.mediaHeight = data.img.h;
                model.mediaWidth = data.img.w;
                model.mediaCover = data.thumb.url;
                model.mediaCoverWidth = data.thumb.w;
                model.mediaCoverHeight = data.thumb.h;
                callback();
            });
        });
    }
};

var sinaVideo = function (uri, model, callback) {
    //通过sinaapp的接口得到优酷视频信息.
    var options = {
        host:'rekey.sinaapp.com',
        port:80,
        path:'/sinav.php?url=' + uri
    };
    console.log('Start get video\'s info.Url is ' + uri);
    http.get(options, function (res) {
        res.on('data', function (data) {
            data = JSON.parse(data.toString());
            console.log(data);
            model.title = data.title;
            if (data.description) {
                model.description = data.description;
            }
            model.tags = data.tags.join(',');
            /** @namespace data.img */
            /** @namespace data.vid */
            model.mediaCover = data.img;
            model.source = data.url;
            model.sourceName = data.type;
            model.mediaUri = data.vid;
            model.mediaCoverWidth = videoCoverWidth;
            model.mediaCoverHeight = videoCoverHeight;
            callback();
        });
    });
};